package com.haoma.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haoma.entity.LearningPath;
import com.haoma.result.Result;
import com.haoma.service.LearningPathService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description:
 */
@RestController
@RequestMapping("/admin/learning")
public class LearningPathController {

    @Autowired
    private LearningPathService learningPathService;

    /**
     * 分页查询
     *
     * @param name
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result pageLearningPath(@PathVariable Integer page, @PathVariable Integer limit, @RequestParam(required = false) String name) {
        IPage iPage = learningPathService.pageLearningPath(page, limit, name);
        return Result.success(iPage);
    }

    /**
     * 新增学习路
     *
     * @param learningPath
     * @return
     */
    @PostMapping("/save")
    public Result saveLearningPath(@RequestBody LearningPath learningPath) {
        learningPathService.saveLearningPath(learningPath);
        return Result.success(learningPath);
    }

    /**
     * 修改
     *
     * @param learningPath
     * @return
     */
    @PutMapping("/update")
    public Result updateLearningPath(@RequestBody LearningPath learningPath) {
        learningPathService.updateLearningPath(learningPath);
        return Result.success();
    }

    @DeleteMapping("/del/{id}")
    public Result delLearningPath(@PathVariable Integer id) {
        learningPathService.delLearningPath(id);
        return Result.success();
    }

    /**
     * 导出excel
     *
     * @param response
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        learningPathService.exportExcel(response);
    }

    /**
     * 导入excel
     */
    @PostMapping("/import")
    public void importExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        learningPathService.importExcel(multipartFile);
    }

    /**
     * 下载excel模板
     */
    @GetMapping("/template")
    public void getExcelTemplate(HttpServletResponse response) {
        learningPathService.getExcelTemplate(response);
    }
}
